package com.lyc.service.impl;

import com.lyc.constant.Constant;
import com.lyc.dao.OrderItemMapper;
import com.lyc.dao.OrderMapper;
import com.lyc.dao.ProductMapper;
import com.lyc.entity.Order;
import com.lyc.entity.OrderItem;
import com.lyc.entity.Product;
import com.lyc.entity.biz.OrderBiz;
import com.lyc.entity.qo.OrderQo;
import com.lyc.entity.vo.*;
import com.lyc.service.IOrderService;
import com.lyc.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper itemMapper;

//    @Autowired
//    private ProductMapper productMapper;

    @Override
    public Order makeOrder(Integer uid, OrderBiz orderBiz) {
        //封装订单实体
        Order order = new Order();
        String oid = UUID.randomUUID().toString().replace("-", "");
        order.setOid(oid);
        if (uid != null) {
            order.setUid(uid);
        }
        order.setFreight(orderBiz.getTotalFreight());
        order.setDate(new Date());
        order.setAddress(orderBiz.getAddress());
        order.setTotal(orderBiz.getTotal());
        orderMapper.insertSelective(order);
        //封装订单项实体
        List<OrderItem> list = null;
        List<ProductVo> productVos = orderBiz.getProductVoList();
        if (CollectionUtil.notEmpty(productVos)) {
            list = new ArrayList<>();
            for (ProductVo productVo : productVos) {
                OrderItem item = new OrderItem();
                Product product = productVo.getProduct();
                item.setOid(oid);
                item.setPid(product.getPid());
                item.setSize(productVo.getSize());
                item.setColor(productVo.getColor());
                int count = productVo.getCount();
                item.setCount(count);
                item.setSubtotal((product.getOffPrice() + product.getFreight()) * count);
                list.add(item);
            }
        }
        itemMapper.insertOrderItems(list);
        return order;
    }

    @Override
    public PageBean<OrderVo> getMyOrdersByStatus(Integer uid, Integer status, Integer currentPage) {
        PageBean<OrderVo> page = new PageBean<>();
        page.setCurrentPage(currentPage);
        page.setPageSize(Constant.ORDER_PAGE_SIZE);//每页显示几条记录
        int totalCount = orderMapper.selectCountByUidAndStatus(uid, status);
        page.setTotalCount(totalCount);
        page.setTotalPage((int) Math.ceil((double)(page.getTotalCount())/page.getPageSize()));
        //封装页面展示实体集合
        List<OrderVo> orderVoList = null;
        List<Order> orderList = orderMapper.selectByUidAndStatusWithPage(uid, status,page.getStart(), page.getPageSize());
        if (CollectionUtil.notEmpty(orderList)) {
            orderVoList = new ArrayList<>();
            for (Order order : orderList) {
                OrderVo orderVo = new OrderVo();
                OrderVo vo = itemMapper.selectByOid(order.getOid());
                orderVo.setOrder(order);
                orderVo.setItemList(vo.getItemList());
                orderVoList.add(orderVo);
            }
        }
        page.setData(orderVoList);
        return page;
    }

    @Override
    public void updateOrderStatusByOid(String oid, Integer status) {
        orderMapper.updateStatusByOid(oid, status);
    }

    @Override
    public Order getOrderByOid(String oid) {
        return orderMapper.selectByPrimaryKey(oid);
    }

    @Override
    public OrderVo getOrderDetailByOid(String oid) {
        OrderVo vo = itemMapper.selectByOid(oid);
        Order order = orderMapper.selectByPrimaryKey(oid);
        if (vo != null)
            vo.setOrder(order);
        return vo;
    }

    @Override
    public void deleteOrderByOid(String oid) {
        itemMapper.deleteByOid(oid);//删除订单前将该订单的订单项一并删除
        orderMapper.deleteByPrimaryKey(oid);
    }

    @Override
    public PageBean<OrderUsernameVo> getOrderByConditions(OrderQo orderQo, Integer currentPage, int pageSize) {
        PageBean<OrderUsernameVo> page = new PageBean<>();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        page.setStart((currentPage-1)*page.getPageSize());
        page.setTotalCount(orderMapper.selectCountByConditions(orderQo));
        page.setTotalPage((int)Math.ceil((double)(page.getTotalCount())/page.getPageSize()));
        orderQo.setStart(page.getStart());
        orderQo.setPageSize(page.getPageSize());
        page.setData(orderMapper.selectByConditions(orderQo));
        return page;
    }

    @Override
    public void updateOrder(Order order) {
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void deleteBatchByOids(List<String> oids) {
        itemMapper.deleteBatchByOids(oids);
        orderMapper.deleteBatchByOids(oids);
    }
}
